package com.yanghui.dtracing.plugin.hystrix;

import com.yanghui.dtracing.core.match.ClassMatch;
import com.yanghui.dtracing.core.match.HierarchyMatch;
import com.yanghui.dtracing.core.plugin.AbstractClassEnhancePluginDefine;
import com.yanghui.dtracing.core.plugin.IConstructorMethodInterceptPoint;
import com.yanghui.dtracing.core.plugin.IInstanceMethodInterceptPoint;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

public class HystrixDefinition extends AbstractClassEnhancePluginDefine {

    @Override
    public ClassMatch enhanceClass() {
        return HierarchyMatch.build(new String[]{"com.netflix.hystrix.HystrixCommand"});
    }

    @Override
    public IConstructorMethodInterceptPoint[] buildConstructorMethodInterceptPoints() {
        return new IConstructorMethodInterceptPoint[]{
            new IConstructorMethodInterceptPoint(){
                @Override
                public ElementMatcher<MethodDescription> buildConstructorMatcher() {
                    return ElementMatchers.any();
                }
                @Override
                public String constructorInterceptor() {
                    return "com.yanghui.dtracing.plugin.hystrix.HystrixConstructorInter";
                }
            }
        };
    }

    @Override
    public IInstanceMethodInterceptPoint[] buildInstanceMethodInterceptPoints() {
        return new IInstanceMethodInterceptPoint[]{
            new IInstanceMethodInterceptPoint(){

                @Override
                public ElementMatcher<MethodDescription> buildMethodsMatcher() {
                    return ElementMatchers.named("run");
                }

                @Override
                public String methodInterceptor() {
                    return "com.yanghui.dtracing.plugin.hystrix.HystrixInstanceMethodAroundInterceptor";
                }
            }
        };
    }
}
